$(function(){
    // 先执行一次加载信息
    loaddate("",1);

    $("#fuzzy").live("click",function () {
        layer.tips('只能根据部门名称进行查询', '#fuzzy', {
            tips: [1, '#0FA6D8'] //还可配置颜色
        });
    });

    $(".prev").live("click",function () {
        var keyword = $("#fuzzy").val();
        // 计算上一页
        var pageNo = $(".current_num").text();
        pageNo = parseInt(pageNo);
        if (pageNo == undefined || pageNo <= 1){
            pageNo = 1;
        }else{
            pageNo = pageNo - 1;
        }
        // 加载数据
        loaddate(keyword,pageNo);
    });

    $(".next").live("click",function () {
        var keyword = $("#fuzzy").val();
        var pageNo = $(".current_num").text();
        pageNo = parseInt(pageNo);
        var totalNo = $(".total_num").text();
        totalNo = parseInt(totalNo);
        if (pageNo == undefined){
            pageNo = 1;
        } else if(pageNo >= totalNo){
            pageNo = totalNo;
        }else{
            pageNo = pageNo + 1;
        }
        // 加载数据
        loaddate(keyword,pageNo);
    });

    $(".first").live("click",function () {
        var keyword = $("#fuzzy").val();
        var pageNo = 1;
        // 加载数据
        loaddate(keyword,pageNo);
    });

    $(".last").live("click",function () {
        var keyword = $("#fuzzy").val();
        var pageNo = $(".total_num").text();
        pageNo = parseInt(pageNo);
        if (pageNo == undefined){
            pageNo = 1;
        }
        // 加载数据
        loaddate(keyword,pageNo);
    });
});

// 删除事件
function del(depid) {
    $.ajax({
        url: "/delDepartment",
        data: {department_id:depid},
        type: "POST",
        cache: false,
        success: function(dep){
            loaddate("",1);
        }
    });
}

// 查询单个信息
function sel(depid) {
    window.location.href = "/department/upldepartment.jsp?" + depid;
}

// 模糊查询
function fuzzy() {
    var depName = $("#fuzzy").val();
    loaddate(depName,1);
}

// 加载信息
function loaddate(keyword,pageNo){
    $("#table_tbody").empty();
    $("#fuzzy").val(keyword);
    $.ajax({
        url: "/selectDepartmentAll",
        data:{
             pagenNum: pageNo,
             depname: keyword
        },
        type: "POST",
        dataType: "json",
        timeout: 1000,
        cache: false,
        success: function(department) {
            console.log(department);
            if (department.success){
                var departmentlist = department.data.list;
                for (var i = 0; i < departmentlist.length; i++ ){
                    var departmentall = departmentlist[i];
                    $("#table_tbody").append(
                        "<tr>"+
                        "<td>"+departmentall.depid+"</td>"+
                        "<td>"+departmentall.depname+"</td>"+
                        "<td>"+departmentall.depbrief+"</td>"+
                        "<td>"+
                        "<a href='#'>" +
                        "<button  class='layui-btn layui-btn-sm' onclick='sel("+departmentall.depid+")'><i class='layui-icon'>&#xe642;</i></button>"+
                        "</a>"+
                        "&nbsp;<a href='#'>" +
                        "<button onclick='del("+departmentall.depid+")' class='layui-btn layui-btn-sm'><i class='layui-icon'>&#xe640;</i></button>" +
                        "</a>"+
                        "</td>"+
                        "</tr>"
                    );
                }
                $("#table_tbody").append(
                    "<tr><td colspan='4'><hr /></td></tr>" +
                    "<tr style='border-bottom: 1px solid #AAAAAA; height: 50px;text-align: center;'>" +
                    "<td colspan='4'><div id='page_div'>" +
                    "第 <span class='current_num'>"+department.data.pageNum+"</span>页" +
                    "共 <span class='total_num'>" + department.data.pages + " </span>页" +
                    "每页 "+ department.data.pageSize + " 条信息" +
                    "共 " + department.data.total + " 条信息" +
                    "&nbsp;&nbsp;<a href='javascript:(0);' class='first'>首页</a>" +
                    "&nbsp;&nbsp;<a href='javascript:(0);' class='prev'>上一页</a>" +
                    "&nbsp;&nbsp;<a href='javascript:(0);' class='next'>下一页</a>" +
                    "&nbsp;&nbsp;<a href='javascript:(0);' class='last'>尾页</a>" +
                    "</div></td></tr>");
            }else{
                $("#table_tbody").append("<tr><td>"+aaaaa+"</td></tr>");
            }
        }
    })
}